﻿using ECommon;
using LeanApp.Bll;
using System;
using System.Windows;

namespace LeanApp.View
{
    /// <summary>
    /// RegisterWindow.xaml 的交互逻辑
    /// </summary>
    public partial class RegisterWindow : Window
    {
        /// <summary>
        /// 注册窗口
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        public RegisterWindow(string userInfo)
        {
            InitializeComponent();
            _userInfo = userInfo;
            InitialCtrl();
        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitialCtrl()
        {
            btnRegister.Click += BtnRegister_Click;
        }

        /// <summary>
        /// 发送邮件单击事件
        /// </summary>
        /// <param name="sender">事件对象</param>
        /// <param name="e">事件参数</param>
        private async void BtnRegister_Click(object sender, RoutedEventArgs e)
        {
            _userName = txtUserName.Text.Trim();
            if (string.IsNullOrWhiteSpace(_userName))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            _company = txtCompany.Text.Trim();
            if (string.IsNullOrWhiteSpace(_company))
            {
                MessageBox.Show("公司不能为空");
                return;
            }
            _contract = txtContract.Text.Trim();
            if (string.IsNullOrWhiteSpace(_contract))
            {
                MessageBox.Show("联系方式不能为空");
                return;
            }
            if (!StringTool.RegexIsEmail(_contract) && !StringTool.RegexIsPhone(_contract))
            {
                MessageBox.Show("联系方式需要是邮箱或手机号");
                return;
            }
            if (string.IsNullOrWhiteSpace(_userInfo))
            {
                MessageBox.Show("用户信息不能为空");
                return;
            }
            _location = txtLocation.Text.Trim();
            _position = txtPosition.Text.Trim();
            try
            {
                EmailTool.SendEmail("852585502@qq.com", "endatech6688@163.com", "852585502@qq.com", "mmrredigxlwnbcdh", "smtp.qq.com", 587, "用户注册", UserInfo, true, null);
                EmailBll bll = new EmailBll();
#pragma warning disable CRR0029 // ConfigureAwait(true) is called implicitly
                await bll.SendEmail("852585502@qq.com", "zjw1514@163.com", "852585502@qq.com", "mmrredigxlwnbcdh", "smtp.qq.com", 587, "用户注册", UserInfo, true, null);
#pragma warning restore CRR0029 // ConfigureAwait(true) is called implicitly
                DialogResult = true;
                Close();
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 用户姓名
        /// </summary>
        private string _userName = "";
        /// <summary>
        /// 地址
        /// </summary>
        private string _location = "";
        /// <summary>
        /// 公司
        /// </summary>
        private string _company = "";
        /// <summary>
        /// 联系方式
        /// </summary>
        private string _contract = "";
        /// <summary>
        /// 职位
        /// </summary>
        private string _position = "";
        /// <summary>
        /// 用户信息
        /// </summary>
        private string _userInfo = "";

        /// <summary>
        /// 用户信息
        /// </summary>
        public string UserInfo
        {
            get
            {
                _userInfo = _userInfo.Replace("，", "\r\n");
                return $"用户姓名：{_userName}\r\n公司：{_company}\r\n联系方式：{_contract}\r\n职位：{_position}\r\n地址：{_location}\r\n{_userInfo}";
            }
        }
    }
}
